<?php

/**
 * This is the model class for table "event".
 *
 * The followings are the available columns in table 'event':
 * @property integer $id
 * @property string $name
 * @property string $date
 * @property string $time
 * @property string $place
 * @property integer $id_type
 * @property integer $id_host
 *
 * The followings are the available model relations:
 * @property Comment[] $comments
 * @property Typeevent $idType
 */
class Event extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Event the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'event';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, date, time, place, id_type', 'required', 'on'=>'create'),
			//array('id_type, id_host', 'numerical', 'integerOnly'=>true),
			array('name, place', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, date, time, place, id_type, id_host', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comment', 'event_id'),
			'idType' => array(self::BELONGS_TO, 'Typeevent', 'id_type'),
            'comments' => array(self::HAS_MANY, 'Comment', 'event_id'),
            'commentCount' => array(self::STAT, 'Comment', 'event_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'create_user_id'),
			'likes' => array(self::BELONGS_TO, 'Like', 'id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'date' => 'Date',
			'time' => 'Time',
			'place' => 'Place',
			'id_type' => 'Type',
			'id_host' => 'Id Host',
			'create_user_id'=>'Create User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('place',$this->place,true);
		$criteria->compare('id_type',$this->id_type);
		$criteria->compare('id_host',$this->id_host);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>5,
			),
		));
	}
    
    public function getTypeEvent ()
	{
		$result = array();
		$typeevent = Typeevent::model()->findAll(array('order'=>'name'));
		foreach ($typeevent as $type):
			$result[$type['id_type']]=$type['name'];
		endforeach;
		return $result;
	}
    
    public function findRecentPosts($limit=10)
    {
        $criteria=array(
            'condition'=>'',
            'order'=>'id DESC',
            'limit'=>$limit,
        );
        return $this->findAll($criteria);
    }
	
	protected function beforeValidate ()
	{
		if ($this->isNewRecord)
		{
			$this->create_user_id = Yii::app()->user->id;
		}
		return parent::beforeValidate();
	}
	
	public function view ($id)
	{	
		$model = Like::model()->findByPk($id);
		if ($model->view !=null)
		{
			$view = $model->view + 1;
			if (Like::model()->updateByPk($id, array('view'=>$view)))
				return true;
			else return false;
		}else
		{
			$view = 1;
			if (Like::model()->updateByPk($id, array('view'=>$view)))
				return true;
			else return false;
		}
	}
	public function _view ($id,$i)
	{	
		$model = Like::model()->findByPk($id);
		if ($model->view !=null)
		{
			$view = $model->view - $i;
			if (Like::model()->updateByPk($id, array('view'=>$view)))
				return true;
			else return false;
		}
	}
	
	public function like ($id)
	{
		$model = Like::model()->findByPk($id);
		if ($model->like != null)
		{
			$like = $model->like + 1;
			if (Like::model()->updateByPk($id, array('like'=>$like)))
				return true;
			else return false;
		}else
		{
			$like = 1;
			if (Like::model()->updateByPk($id, array('like'=>$like)))
				return true;
			else return false;
		}
	}
    public function unlike ($id)
	{
		$model = Like::model()->findByPk($id);
		if ($model->like != null)
		{
			$like = $model->like - 1;
			if (Like::model()->updateByPk($id, array('like'=>$like)))
				return true;
			else return false;
		}else
		{
			$like = 1;
			if (Like::model()->updateByPk($id, array('like'=>$like)))
				return true;
			else return false;
		}
	}
    public function sameEvent($id,$id_type)
    {
        $criteria=array(
            'condition'=>'id_type='.$id_type.' and id!='.$id,
            'order'=>'id DESC',
            'limit'=>'5',
        );
        return $this->findAll($criteria);
    }
}